package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.utils.StringUtils;
import com.bwie.domain.PrizeEs;
import com.bwie.domain.req.PrizeEsReq;
import com.bwie.mapper.EsPrizeMapper;
import com.bwie.service.EsPrizeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class EsPrizeServiceImpl implements EsPrizeService {

    @Autowired
    private EsPrizeMapper esPrizeMapper;

    /**
     * 创建索引
     */
    @Override
    public void creatIndex() {
        esPrizeMapper.createIndex();
    }

    /**
     * 根据查询条件分页检索奖品信息
     *
     * @param prizeEsReq 前端传来的查询参数封装
     * @return 分页后的奖品 ES 实体列表
     */
    @Override
    public PageInfo<PrizeEs> prizeList(PrizeEsReq prizeEsReq) {

        // 1. 构造 ES 专用 Lambda 查询包装器
        LambdaEsQueryWrapper<PrizeEs> wrapper = new LambdaEsQueryWrapper<>();

        // 2. 如果奖品名称不为空，则添加 match 查询（支持分词）
        if (StringUtils.isNotBlank(prizeEsReq.getPrizeName())) {
            wrapper.match(PrizeEs::getPrizeName, prizeEsReq.getPrizeName());
        }
        //3.活动id不能为空
        if (prizeEsReq.getLottoId()!=null){
            wrapper.eq(PrizeEs::getLottoId,prizeEsReq.getLottoId());
        }
        //分页查询
        EsPageInfo<PrizeEs> esPageInfo = esPrizeMapper.pageQuery(wrapper, prizeEsReq.getPageNum(), prizeEsReq.getPageSize());
        PageInfo<PrizeEs> prizeEsPageInfo = new PageInfo<>(esPageInfo.getList());

        return prizeEsPageInfo;
    }

    @Override
    public void insertBarch(ArrayList<PrizeEs> list) {
        esPrizeMapper.insertBatch(list);
    }


}
